﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            ArrayList ar = new ArrayList { 1, 2, 3,4,5,6 };
            int x = 21;
            ArrayList res = new ArrayList();
            ArrayList operat = new ArrayList();
            ArrayList ways = new ArrayList();
            res.Add(1);
            for (int i = 1; i < ar.Count; i++)
            {
                int N = (int)ar[i];
                res = DoMaths(ar, N, res);
            }
            ArrayList indexAr = GetIndexes(x, res);
            foreach (int index in indexAr)
            {
                ArrayList operators = new ArrayList();
                FindOperators(res.Count, index, operators);
            }
            int count = indexAr.Count;
            Console.WriteLine(count+ " ways to get  " +x);
            ArrayList znaki = new ArrayList();
            foreach (var elem in indexAr)
            {
                FindOperators(res.Count, (int)elem, znaki);
            }
            int e = 0; 
            int j = 0;
            while (e < 5)
            {
                while (j < znaki.Count)
                {
                    Console.Write(ar[e] + " ");
                    Console.Write(znaki[j] + " ");
                    break;

                }
                j++; 
                e++;
            }
          
            Console.Write(ar[ar.Count-1] + " ");
            Console.WriteLine("= " + x);
            Console.WriteLine();
        }


        static public ArrayList DoMaths(ArrayList ar, int N, ArrayList res)
        {
            ArrayList operatCopy = new ArrayList();
            ArrayList resCopy = new ArrayList();
            foreach (var el in res)
            {
                resCopy.Add(el);
            }
            res.Clear();
            foreach (int el in resCopy)
            {
                res.Add(el + N);
                res.Add(el - N);
                res.Add(el * N);
                res.Add(el / N);
            }
            return res;
        }


        static public ArrayList GetIndexes(int x, ArrayList res)
        {
            ArrayList indexAr = new ArrayList();
            for (int i = 0; i < res.Count; i++)
            {
                int index = (int)res[i];
                if (index == x)
                {
                    indexAr.Add(i);
                }
            }
            return indexAr;
        }


        static public void FindOperators(int count, int elem, ArrayList operators) // передаю индекс инта в массиве результатов
        {
            int numElemInGroup = count / 4;
            int groupNum = elem / numElemInGroup;

            if (groupNum == 0)
            {
                operators.Add("+");
            }
            else if (groupNum == 1)
            {
                operators.Add("-");
            }
            else if (groupNum == 2)
            {
                operators.Add("*");
            }
            else if (groupNum == 3)
            {
                operators.Add("/");
            }

            if (numElemInGroup == 1)
            {
                return;
            }
            elem -= groupNum * numElemInGroup;
            FindOperators(numElemInGroup, elem, operators);
        }
    }
}